import { ajax } from "../js/init";
import { propName } from "./interface";
import { ajaxPromisestringify, ajaxReg } from "./type";
export default class MyAjax {
  public url: string;
  constructor(url: string) {
    this.url = MyAjax.url(url);
  }
  static reg1: ajaxReg = /^http:\/\//;
  static reg2: ajaxReg = /^https:\/\//;
  static url(v: string): string {
    if (!v) throw "填写地址";
    if (MyAjax.reg1.test(v) || MyAjax.reg2.test(v)) return v;
    return "http://" + v;
  }
  //get请求
  static get(url: string, data?: propName): Promise<ajaxPromisestringify> {
    return new Promise((resolve, reject): ajaxPromisestringify => {
      if (typeof url !== "string") throw "url类型错误";
      if (!MyAjax.reg1.test(url) && !MyAjax.reg2.test(url))
        url = "http://" + url;
      if (data) {
        let str = "?";
        for (let key in data) {
          str += key + "=";
          str += data[key] + "&";
        }
        url += str.slice(0, str.length - 1);
      }
      const xhr = new XMLHttpRequest();
      xhr.onreadystatechange = () => {
        if (xhr.readyState === 4 && xhr.status)
          return resolve(xhr.responseText);
      };
      xhr.open("GET", url);
      try {
        xhr.send();
      } catch (error) {
        console.warn("错误:", error);
      }
    });
  }
  static post(url: string, data?: propName): Promise<ajaxPromisestringify> {
    return new Promise((resolve, reject): ajaxPromisestringify => {
      if (typeof url !== "string") throw "url类型错误";
      if (!MyAjax.reg1.test(url) && !MyAjax.reg2.test(url))
        url = "http://" + url;
      let value;
      if (data) {
        let str = "";
        for (let key in data) {
          str += key + "=";
          str += data[key] + "&";
        }
        value = str.slice(0, str.length - 1);
      }
      const xhr = new XMLHttpRequest();
      xhr.onreadystatechange = () => {
        if (xhr.readyState === 4 && xhr.status)
          return resolve(xhr.responseText);
      };
      xhr.open("POST", url);
      if (!(data instanceof FormData)) {
        xhr.setRequestHeader(
          "Content-type",
          "application/x-www-form-urlencoded"
        );
        xhr.send(value);
      } else {
        try {
          xhr.send(data);
        } catch (error) {
          console.warn("错误: ", error);
        }
      }
    });
  }
  get(url: string, data?: propName): Promise<ajaxPromisestringify> {
    return new Promise((resolve, reject): ajaxPromisestringify => {
      if (typeof url !== "string") throw "url类型错误";
      if (!MyAjax.reg1.test(url) && !MyAjax.reg2.test(url))
        url = this.url + url;
      if (data) {
        let str = "?";
        for (let key in data) {
          str += key + "=";
          str += data[key] + "&";
        }
        url += str.slice(0, str.length - 1);
      }
      const xhr = new XMLHttpRequest();
      xhr.onreadystatechange = () => {
        if (xhr.readyState === 4 && xhr.status)
          return resolve(xhr.responseText);
      };
      xhr.open("GET", url);
      try {
        xhr.send();
      } catch (error) {
        console.warn("错误:", error);
      }
    });
  }
  //post请求&文件上传
  post(url: string, data?: propName): Promise<ajaxPromisestringify> {
    return new Promise((resolve, reject): ajaxPromisestringify => {
      if (typeof url !== "string") throw "url类型错误";
      if (!MyAjax.reg1.test(url) && !MyAjax.reg2.test(url))
        url = this.url + url;
      let value;
      if (data) {
        let str = "";
        for (let key in data) {
          str += key + "=";
          str += data[key] + "&";
        }
        value = str.slice(0, str.length - 1);
      }
      const xhr = new XMLHttpRequest();
      xhr.onreadystatechange = () => {
        if (xhr.readyState === 4 && xhr.status)
          return resolve(xhr.responseText);
      };
      xhr.open("POST", url);
      if (!(data instanceof FormData)) {
        xhr.setRequestHeader(
          "Content-type",
          "application/x-www-form-urlencoded"
        );
        xhr.send(value);
      } else {
        try {
          xhr.send(data);
        } catch (error) {
          console.warn("错误: ", error);
        }
      }
    });
  }
  put(url: string, data?: propName): Promise<ajaxPromisestringify> {
    return new Promise((resolve, reject): ajaxPromisestringify => {
      if (typeof url !== "string") throw "url类型错误";
      if (!MyAjax.reg1.test(url) && !MyAjax.reg2.test(url))
        url = this.url + url;
      if (data) {
        let str = "?";
        for (let key in data) {
          str += key + "=";
          str += data[key] + "&";
        }
        url += str.slice(0, str.length - 1);
      }
      const xhr = new XMLHttpRequest();
      xhr.onreadystatechange = () => {
        if (xhr.readyState === 4 && xhr.status)
          return resolve(xhr.responseText);
      };
      xhr.open("PUT", url);
      xhr.send();
    });
  }
  delete(url: string, data?: propName): Promise<ajaxPromisestringify> {
    return new Promise((resolve, reject): ajaxPromisestringify => {
      if (typeof url !== "string") throw "url类型错误";
      if (!MyAjax.reg1.test(url) && !MyAjax.reg2.test(url))
        url = this.url + url;
      if (data) {
        let str = "?";
        for (let key in data) {
          str += key + "=";
          str += data[key] + "&";
        }
        url += str.slice(0, str.length - 1);
      }
      const xhr = new XMLHttpRequest();
      xhr.onreadystatechange = () => {
        if (xhr.readyState === 4 && xhr.status)
          return resolve(xhr.responseText);
      };
      xhr.open("GET", url);
      xhr.send();
    });
  }
}
